package cn.itsource.aop;

import cn.itsource.annotation.MyLog;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * <p>切面类：处理日志</p>
 *
 * @author 波波老师(微信 ： javabobo0513)
 * @date 2022/11/16-18:16
 */
@Aspect
@Component
public class MyLogAop {

    /**
     * 1、通过注解
     * 2、通过路径
     */

    //定义一个切点
    @Pointcut(value="@annotation(myLog)")
    public void cutpoint(MyLog myLog){
    }
    /**
     * 环绕通知
     * 前置通知
     * 后置通知
     * 异常通知
     */
    /**
     * 环绕通知
     */
    @Around("cutpoint(myLog)")
    public Object around(ProceedingJoinPoint joinPoint, MyLog myLog) throws Throwable {
        String urlPath = joinPoint.getSignature().toString();

        long t1 = System.currentTimeMillis();

        //执行目标方法
        Object proceed = joinPoint.proceed();

        long t2 = System.currentTimeMillis();
        System.out.println("方法执行耗时：" + (t2-t1));
        System.out.println("方法路径：" + urlPath);

        //日志信息入库

        //Log log = new Log()
        //log.seturlPath(urlPath);
        //log.setTime(new Date());
        //log.set.....
        //log.set.....
        //log.set.....
        //log.set.....
        //log.set.....
        //logMapper.save(log);

        return proceed;
    }
}